# encoding=utf-8
import os


# import fcntl
#
#
# def get_host_ip(ifname):
#     """
#     获取主机IP地址
#     :param ifname: 端口的名称
#     :return:
#     """
#     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#     return socket.inet_ntoa(fcntl.ioctl(
#         s.fileno(),
#         0x8915,  # SIOCGIFADDR
#         struct.pack('256s', ifname[:15])
#     )[20:24])


def add_host_ip(ip_address, ifname):
    """
    给本机指定端口增加IP地址
    :param ip_address:
    :param ifname: 端口的名称
    :return:
    """
    run_cmd("ip addr add %s dev %s" % (ip_address, ifname))


def run_cmd(cmd):
    """
    运行命令
    :param cmd:
    :return:
    """
    os.system(cmd)
